package imusic.baidu;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import java.net.URL;

public class SongsOfTop200Singer {
    private static Log log = LogFactory.getLog(SongsOfTop200Singer.class);
    static String savaPath = "/home/h/BaiduTop200SingerSongs.txt";
    static int threadNum = 100;


    public static void sava() throws Exception {
        Util.deleteOldFile(savaPath);
        for (int i = 0; i < threadNum; i++) {
            SongsOfTop200SingerThread thead = new SongsOfTop200SingerThread();
            thead.start();
        }

    }


    public static void CompleteSongNum(Singer singer) throws Exception {
        HtmlCleaner cleaner = new HtmlCleaner();
        TagNode tagNode = cleaner.clean(new URL(
                "http://music.baidu.com/artist/" + singer.getId()), "utf8");
        Object[] singerIds = tagNode
                .evaluateXPath("//li[@class='ui-tab-active  ']/a/text()");
        String songNum = singerIds[0].toString().trim().replaceAll("歌曲", "")
                .replaceAll("\\(", "").replaceAll("\\)", "");
        singer.setSongNum(songNum);
    }

    public static StringBuffer getSongsBySinger(Singer singer) throws Exception {
        StringBuffer buffer = new StringBuffer();
        HtmlCleaner cleaner = new HtmlCleaner();
        int songNum = Integer.valueOf(singer.getSongNum());
        int page = songNum / 20 + 1;
        for (int i = 0; i < page; i++) {
            String url = "http://music.baidu.com/data/user/getsongs?start=" + i
                    * 20 + "&ting_uid=" + singer.getId() + "&order=hot";

            long time3 = System.currentTimeMillis();
            String songs = Util.getUrlContent(url);
            log.info("get detail 20 song use  time---" + (System.currentTimeMillis() - time3) + "---" + Thread.currentThread());

            String songshtml = Util.getHtmlFromJson(songs);

            TagNode tagNode = cleaner.clean(songshtml);
            Object[] songName = tagNode
                    .evaluateXPath("//span[@class='song-title ']/text()");
            Object[] albumName = tagNode
                    .evaluateXPath("//span[@class='album-title']/text()");
            for (int j = 0; j < albumName.length; j++) {
                buffer.append(singer.getName());
                buffer.append("|");
                buffer.append(i * 20 + j + 1);
                buffer.append("|");
                buffer.append(songName[j].toString().trim());
                buffer.append("|");
                buffer.append(albumName[j].toString().trim());
                buffer.append("\n");
            }
        }

        return buffer;

    }


}
